package com.qianfeng.smsplatform.search.service.impl;

/**
 * @author fjw
 * @version 1.0
 * @date 2019/12/3 11:08
 */

import com.qianfeng.smsplatform.common.constants.CacheConstants;
import com.qianfeng.smsplatform.common.model.Standard_Submit;
import com.qianfeng.smsplatform.search.feign.MyFeign;
import com.qianfeng.smsplatform.search.service.FilterChain;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * todo 暂时还 不知道这个过滤器的作用 这个过滤器的作用是补全字段
 */
@Slf4j
@Component
public class SmsOpIDFilter implements FilterChain {

    //注入feign
    private MyFeign myFeign;

    @Override
    public boolean doFilter(Standard_Submit submit) {
        // 这个对象里面有已经填了几个参数
        log.info("执行了字段补全过滤器");
        //通过这个实体类我们可以吧 手机号
        // clinet id
        int clientID = submit.getClientID();
        //消息内容
        String messageContent = submit.getMessageContent();
        //发送方式  HTTP web
        Integer source = submit.getSource();
        //目的手机号
        String destMobile = submit.getDestMobile();

        // 取出手机号的前7位
        if (destMobile!=null){
            String deMobile = destMobile.substring(0, 6);
            // 调动缓存服务 查询到省标识 城市标识
            String phase = myFeign.getPhase(CacheConstants.CACHE_PREFIX_PHASE + deMobile);
            String[] split = phase.split("&");
            if (split!=null&&split.length>2){
                Integer provId= Integer.parseInt(split[0]);
                Integer cityId= Integer.parseInt(split[1]);
                submit.setProvinceId(provId);
                submit.setCityId(cityId);
            }
        }

        //根据clientId  查询 t_client_channel 得到扩展码 extendNumber
        // price  channelId
        Map<Object, Object> map = myFeign.getClientById(CacheConstants.CACHE_PREFIX_CLIENT + clientID);
        Object extendNumber = map.get("extendNumber");
        Object price = map.get("price");
        String channelId = (String) map.get("channelId");
        // 根据channelId 查询通道信息表  能够查询到通道名称 通道类型 账户接入号
        Map<Object, Object> channel = myFeign.getChannelRouter(channelId);
        Object channelName = channel.get("channelName");
        Integer channelType = (Integer) channel.get("channelType");
        Object spNumber = channel.get("spNumber");
        Object protocalType = channel.get("protocalType");
        submit.setOperatorId(channelType);

        return true;
    }
}
